const bodyParser = require('body-parser')
const JWT = require('jsonwebtoken')
const data = require('./mock')
const userList = [{
	username: 'admin',
	password: 'admin'
}, {
	username: 'user',
	password: '123'
}]

module.exports = app => {
	app.use(bodyParser.json())
	app.post('/api/user/login', (req, res) => {
		const { username, password } = req.body
		const currUser = userList.find(v => v.username === username && v.password === password) 
		
		if(currUser) {
			res.send({
				code: 200,
				token: JWT.sign({ username }, '666666')
			})
		} else {
			res.send({
				code: 403
			})
		}
	})
	app.get('/api/user/list', (req, res) => {
		res.send({
			code: 200,
			data: data.list
		})
	})

	app.post('/api/user/add', (req, res) => {
		data.list.push(req.body)
		res.send({
			code: 200,
		})
	})
}